/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.cashier.logic;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import mscs.data.IFileDataStore;
import mscs.data.IGoodsDiscountProxy;
import mscs.data.IGoodsGiftProxy;
import mscs.data.IGoodsProxy;
import mscs.data.IReturnRecordProxy;
import mscs.data.ISaleRecordProxy;
import mscs.data.ITempReturnGoodsProxy;
import mscs.data.pojo.Goods;
import mscs.data.pojo.GoodsDiscount;
import mscs.data.pojo.GoodsGift;
import mscs.data.pojo.ReturnRecord;
import mscs.data.pojo.SaleRecord;

/**
 *
 * @author Cake
 */
public class ReturnGoodsUtil {

        List<Goods> goodsList= new ArrayList<Goods>();                 //待退商品
        List<Goods> returningList= new ArrayList<Goods>();              //正在退货的商品
        List<GoodsGift> returnGiftList=new ArrayList<GoodsGift>();     //正在退货的赠品
        List<Double> priceList=new ArrayList<Double>();        
        List<GoodsGift> giftList=new ArrayList<GoodsGift>();
        List<ReturnRecord> returnRecordList=new ArrayList<ReturnRecord>();
        
        private SaleRecord tempSaleRecord=new SaleRecord();
        
         
        private ArrayList<Integer> Numbers = new ArrayList<Integer>();  //对应退货商品的数量
        
        private IFileDataStore allSale = new ISaleRecordProxy();
        private List<SaleRecord> saleRecordList=(ArrayList<SaleRecord>)(allSale.getObjects());
         
	private IFileDataStore allgoods = new IGoodsProxy();
        private List<Goods> allgoodslist= (List<Goods>)allgoods.getObjects();        
        
         private IFileDataStore allGoodsDiscount=new IGoodsDiscountProxy();
        private List<GoodsDiscount> allGoodsDiscountList= (List<GoodsDiscount>)allGoodsDiscount.getObjects();
        
        
        private IFileDataStore allGifts=new IGoodsGiftProxy();
        private List<GoodsGift> allGiftsList= (List<GoodsGift>)allGifts.getObjects(); 
        
        private IFileDataStore allReturn=new IReturnRecordProxy();
        private List<ReturnRecord> allReturnRecordList= (List<ReturnRecord>)allReturn.getObjects(); 
        
        private IFileDataStore tempGoodsList=new ITempReturnGoodsProxy();//考虑到退货时有一个search，所以新加一个临时的文件
        
     
        
        
        public ReturnGoodsUtil(){
        
        }
        
        public List<Double> getPriceList(){
            return priceList;
        }
        
        public List<Goods> getGoodsList(){
            return goodsList;
        }
        

     //更新待退商品搜索列表   
         public Vector<List> searchGoodsTableVector(String key,String value) {       
         return new Vector<List>(toGoodsDataTable(tempGoodsList.searchObjectsByAttr(key, ".*" + value + ".*")));
    }
        
    //更新搜索列表    
     public Vector<List> searchSaleRecordTableVector(String key,String value) {       
         return new Vector<List>(toSearchSaleRecordDataTable(allSale.searchObjectsByAttr(key, ".*" + value + ".*")));
    }
     
     //更新销售记录中的货物列表
      public Vector<List> goodsTableVector() {       
         return new Vector<List>(toGoodsDataTable(goodsList));
    }
      //更新销售记录中的礼品列表
      public Vector<List>  giftsTableVector(){
          return new Vector<List>(toGiftDataTable(giftList));
      }
      //更新退货记录列表
      public Vector<List>  returnRecordTableVector(){
          return new Vector<List>(toReturnRecordDataTable(returnRecordList));
      }
      //////////////////////////////////
      /*结算的dialog*/
     //更新待退货列表
      public Vector<List> returningTableVector(){
          return new Vector<List>(toGoodsDataTable(returningList));
      }
      //更新礼物列表
      public Vector<List> returnGiftTableVector(){
          return new Vector<List>(toGiftDataTable(returnGiftList));
      }

      
      public SaleRecord getSaleRecord(){
          return tempSaleRecord;
      }
     public List<Goods> getReturningList(){
         return returningList;
    }
     
     public List<GoodsGift> getReturnGiftList(){
         return returnGiftList;
    }
     
     public void clearTemp(){
         List<GoodsGift> newlist=new ArrayList<GoodsGift>(); 
         tempGoodsList.update(newlist);
     }
      
     //加入等待退货的列表
     public boolean setSaleTable(SaleRecord newSaleRecord){
         
         for(SaleRecord saleRecord:saleRecordList){
             
             if(newSaleRecord.getId().equals(saleRecord.getId())){
                 
                 newSaleRecord.setTotalPrice(saleRecord.getTotalPrice());
                 newSaleRecord.setGoodsList(saleRecord.getGoodsList());
                 newSaleRecord.setGiftsList(saleRecord.getGiftsList()); 
                 
                 
                 goodsList=newSaleRecord.getGoodsList(); /////
                 
                 giftList=newSaleRecord.getGiftsList();////////得到销售出去的礼物列表
                 for(int i=1;i<allReturnRecordList.size();i++){
                        if(allReturnRecordList.get(i).getSaleRecordID().equals(newSaleRecord.getId()))
                             returnRecordList.add(allReturnRecordList.get(i));
                         }                            ///////得到该销售的退货记录列表
                 
                 
                 tempGoodsList.update(goodsList);//////最后要加一个。update（new List<Goods>的方法来清空文件）
                 tempSaleRecord=newSaleRecord;
                 //实际剩余能退货数量为减去以往退货的数量
                    for(ReturnRecord returnRecord:returnRecordList){
                        if(returnRecord.getSaleRecordID().equals(newSaleRecord.getId())){
                            for(Goods goods:goodsList){
                                for(Goods good:returnRecord.getGoodsList()){
                                    if(goods.getId().equals(good.getId())){
                                        goods.setNum(goods.getNum()-good.getNum());
                                    }
                                }
                            }
                        }
                    }
                
            return true;
             }          
         }
         return false;
     }
      
     
     //把一个商品加入到退货列表并更新库存信息
     public boolean addToReturnList(Goods newReturnGoods,int num) {
         for(Goods goods:allgoodslist){     
             if((newReturnGoods.getId().equals(goods.getId()))&&(isEnough(goods,num))){ 
       
                 goods.setNum(goods.getNum()+num);//设置数量，加上退掉的
            
                 newReturnGoods.setNum(num);
                 newReturnGoods.setDesc(goods.getDesc());
                 newReturnGoods.setSalPrice(goods.getSalPrice());
                 
                 for(GoodsDiscount goodsdiscount:allGoodsDiscountList){
                    if(goodsdiscount.getId().equals(newReturnGoods.getId())&&(goodsdiscount.getEndDate().after(new Date()))&&(goodsdiscount.getStartDate().before(new Date()))){
                             this.priceList.add((double)num*goodsdiscount.getDiscountPrice());}
                       else{ this.priceList.add((double)num*newReturnGoods.getSalPrice());}
                     }
                 
                 for(GoodsGift goodsGift:allGiftsList){
                     if(goodsGift.getGoodsID().equals(newReturnGoods.getId())){
                         this.returnGiftList.add(goodsGift);
                     }
                 }
                 
                 this.Numbers.add(num);
                 this.returningList.add(newReturnGoods);
                 
                 updateStore(returningList);
                 return true;
             }
         } 
         return false;
    }
     
     
     
     
     public boolean isEnough(Goods goods,int num){
         return goods.getNum()>=num;
     }
     
    //在一次退货完成后更新库存列表
    public void updateStore(List<Goods> list) {  
        for(int i=0; i<allgoodslist.size(); i++){
            for(int j=0;j<list.size();j++){
                if(list.get(j).getId().equals(allgoodslist.get(i).getId())){
                   allgoods.update(allgoodslist);
               }
           }
        }
    }
    
    

    
    public Vector<Vector<String>> toGiftDataTable(List<GoodsGift> list) {
    	try {
            Vector<GoodsGift> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (GoodsGift gifts : seList)
            {
                reList.add((Vector<String>)toGiftsStringArray(gifts));
            }
            return reList;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
    
      public Vector<Vector<String>> toGoodsDataTable(List<Goods> list) {
    	try {
            Vector<Goods> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (Goods goods : seList)
            {
                reList.add((Vector<String>)toGoodsStringArray(goods));
            }
            return reList;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
      
      
       public Vector<Vector<String>> toSearchSaleRecordDataTable(List<SaleRecord> list) {
          
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for(SaleRecord saleRecord:list){
                reList.add((Vector<String>)toSearchSaleRecordStringArray(saleRecord));   
            }
            return reList;  
        }
       
       public Vector<Vector<String>> toReturnRecordDataTable(List<ReturnRecord> list) {
          
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for(ReturnRecord returnRecord:list){
                reList.add((Vector<String>)toReturnRecordStringArray(returnRecord));   
            }
            return reList;  
        }
       
       
       public List<String> toGiftsStringArray(GoodsGift gifts){
        Vector<String> list = new Vector<String>();
        list.add(gifts.getGiftID());
        list.add(String.valueOf(gifts.getGiftSum()));
        return list;
    }
      
    
     public List<String> toGoodsStringArray(Goods goods){
        Vector<String> list = new Vector<String>();
        list.add(goods.getId());
        list.add(String.valueOf(goods.getNum()));
        list.add(goods.getDesc());
        list.add(((Double)goods.getSalPrice()).toString());
        return list;
    }
    
    public List<String> toSearchSaleRecordStringArray(SaleRecord salerecord){
        Vector<String> list = new Vector<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        list.add(salerecord.getId());     
        list.add(((Double)salerecord.getTotalPrice()).toString());
        list.add(sdf.format(salerecord.getDate()));    
        return list;
    }
    
     public List<String> toReturnRecordStringArray(ReturnRecord returnrecord){
        Vector<String> list = new Vector<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        list.add(returnrecord.getId());     
        list.add(((Double)returnrecord.getTotalPrice()).toString());
           
        return list;
    }


}
